(*
  Copyright (c) 2009 Barry Schwartz

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*)

open XNum
open Logging
open Dim
open Box
  
(* The maximal badness tolerated by make. *)
let max_v_badness = ref (num_of_int 1000)
  
(*
  |calc_vert_dimension <boxes>| returns the triple (<top>, <height>, <bottom>)
  where <top> is the height of the first box, <bottom> is the depth of the last box,
  and <height> is the sum of the remaining heights and depths.
*)
let calc_vert_dimensions boxes =
  match boxes with
  | [] -> (dim_zero, xdim_zero, dim_zero)
  | b :: bs ->
      let top = b.b_height in
      let rec iter height depth boxes =
        (match boxes with
         | [] -> (top, height, depth)
         | b :: bs ->
             iter (xdim_add_dim (xdim_add_dim height depth) b.b_height)
               b.b_depth bs)
      in iter xdim_zero b.b_depth bs
  
let calc_height boxes =
  let (top, height, bottom) = calc_vert_dimensions boxes
  in ((xdim_to_dim (xdim_add_dim height top)), bottom)
  
let make boxes =
  let (h, d) = calc_height boxes in
  let rec iter width height result boxes =
    match boxes with
    | [] -> new_compound_box width (xdim_to_dim height) d result
    | b :: bs ->
        (match b.b_contents with
         | CommandBox (`GfxCmd c) -> iter width height (c :: result) bs
         | _ ->
             let v_pos = xdim_add_dim height b.b_depth
             in
               iter (dim_max width b.b_width)
                 (xdim_add_dim (xdim_add_dim height b.b_height) b.b_depth)
                 ((Graphic.PutBox (dim_zero,
                     xdim_select_order v_pos h.d_stretch_order
                       h.d_shrink_order,
                     b)) ::
                   result)
                 bs)
  in iter dim_zero xdim_zero [] (List.rev boxes)
  
let layout_scaled (factor, order) boxes =
  if boxes = []
  then new_compound_box dim_zero dim_zero dim_zero []
  else
    (let ratio =
       if (lt_num factor (num_of_int (-1))) && (order = 0)
       then ((num_of_int (-1)), 0)
       else (factor, order)
     and bad = dim_scale_badness (factor, order) in
     let boxes = List.rev boxes in
     let depth = (scale_box_vert (List.hd boxes) ratio).b_depth
     in
       (if gt_num bad !max_v_badness
        then
          (if lt_num factor num_zero
           then log_string "\nWarning: Overfull vbox (badness "
           else log_string "\nWarning: Underfull vbox (badness ";
           if lt_num bad infinite then log_num bad else log_string "infinite";
           log_string ")!\n")
        else ();
        let rec iter width height result boxes =
          match boxes with
          | [] ->
              new_compound_box width (fixed_dim height.xd_base) depth result
          | b :: bs ->
              (match b.b_contents with
               | CommandBox (`GfxCmd c) -> iter width height (c :: result) bs
               | _ ->
                   let box = scale_box_vert b ratio in
                   let v_pos = xdim_add_dim height b.b_depth
                   in
                     iter (dim_max width box.b_width)
                       (xdim_add_dim (xdim_add_dim height box.b_height)
                          box.b_depth)
                       ((Graphic.PutBox (dim_zero, fixed_dim v_pos.xd_base,
                           box)) ::
                         result)
                       bs)
        in iter dim_zero xdim_zero [] boxes))
  
let to_top box =
  let rec get_baseline cmds =
    match cmds with
    | [] -> num_zero
    | Graphic.PutBox (_, y, _) :: _ -> minus_num y.d_base
    | _ :: cs -> get_baseline cs
  in
    match box.b_contents with
    | CompBox cmds -> shift_compound_box box num_zero (get_baseline cmds)
    | _ -> box
  
let make_to height boxes =
  let (h, _) = calc_height boxes in
  let ratio = adjustment_ratio h height in layout_scaled ratio boxes
  
let make_scaled factor boxes =
  let (h, _) = calc_height boxes in
  let height = mult_num factor h.d_base in
  let ratio = adjustment_ratio h height in layout_scaled ratio boxes
  
let make_spread amount boxes =
  let (h, _) = calc_height boxes in
  let height = add_num amount h.d_base in
  let ratio = adjustment_ratio h height in layout_scaled ratio boxes
  
let make_top boxes = to_top (make boxes)
  
let make_top_to height boxes =
  let (h, _) = calc_height boxes in
  let ratio = adjustment_ratio h height in to_top (layout_scaled ratio boxes)
  
let make_top_scaled factor boxes =
  let (h, d) = calc_height boxes in
  let height = mult_num factor (add_num h.d_base d.d_base) in
  let ratio = adjustment_ratio (dim_add h d) height
  in to_top (layout_scaled ratio boxes)
  
let make_top_spread amount boxes =
  let (h, _) = calc_height boxes in
  let height = add_num amount h.d_base in
  let ratio = adjustment_ratio h height in to_top (layout_scaled ratio boxes)
  

